<?php

namespace app\api\controller;

use app\common\lib\Param;
use app\common\lib\Show;
use app\common\lib\Str;
use app\Request;
use think\facade\Cache;

class User extends AuthBase
{
    //获取user信息
    public function index()
    {
        $user = (new \app\common\business\User())->getNormalUserByField('id',$this->userId,'id,name,phone_number,id_card,sex,avatar,status,autherized');
        return Show::success($user, "OK");
    }

    //获取用户中心顶部统计
    public function getTopStatistics(){
        $userBis = new \app\common\business\User();
        $data = $userBis->getTopStatistic($this->userId);
        return Show::success($data, "OK");
    }

    //更新user表信息
    public function update(){
        if (!$this->request->isPost()) {
            return Show::error([],"非法请求");
        }

        $name = $this->request->param("name", "", "trim");
        $id_card = $this->request->param("id_card", "", "trim");

        //参数校验
        $data = [
            'name' => $name,
            'id_card' => $id_card,
        ];
        $validate = new \app\api\validate\User();
        if (!$validate->scene('update')->check($data)) {
            return Show::error([], $validate->getError());
        }

        $userBisObj = new \app\common\business\User();
        $data['autherized'] = 1;
        $data['sex'] = Str::getSexByIdCard($id_card);
        try {
            $user = $userBisObj->updateData($this->userId,$data);
        } catch (\Exception $e) {
            return Show::error([], $e->getMessage());
        }
        if (!$user) {
            return Show::error([], "更新失败");
        }

        return Show::success([], "更新成功");
    }

    //更新手机号
    public function updatePhone(){
        if (!$this->request->isPost()) {
            return Show::error([],"非法请求");
        }

        $jsCode = $this->request->param("code", "", "trim"); //wx.login()获取的code
        //参数校验
        $data = [
            'js_code' => $jsCode,
        ];
        $validate = new \app\api\validate\User();
        if (!$validate->scene('wechat_login')->check($data)) {
            return Show::error([], $validate->getError());
        }

        $userBisObj = new \app\common\business\User();
        try {
            $user = $userBisObj->dealPhoneNumber($this->userId,$data);
        } catch (\Exception $e) {
            return Show::error([], $e->getMessage());
        }
        if (!$user) {
            return Show::error([], "更新失败");
        }

        return Show::success([], "更新成功");
    }

    //更新头像
    public function updateAvatar(){
        if (!$this->request->isPost()) {
            return Show::error([],"非法请求");
        }

        $avatar = $this->request->param("avatar", "", "trim"); //wx.login()获取的code
        //参数校验
        $data = [
            'avatar' => $avatar,
        ];
        $validate = new \app\api\validate\User();
        if (!$validate->scene('update_avatar')->check($data)) {
            return Show::error([], $validate->getError());
        }

        $userBisObj = new \app\common\business\User();
        try {
            $user = $userBisObj->updateData($this->userId,$data);
        } catch (\Exception $e) {
            return Show::error([], $e->getMessage());
        }
        if (!$user) {
            return Show::error([], "更新失败");
        }

        return Show::success([], "更新成功");
    }

    //通过工人id查询工人详情
    public function getUserInfoById(){
        $id = $this->request->param("id", 0, "intval");

        $userBis = new \app\common\business\User();
        $userInfoBis = new \app\common\business\UserInfo();

        $user = $userBis->getNormalUserByField('id',$id,'id,name,phone_number,sex,avatar,status,autherized,id_card');
        if (empty($user)) return Show::error([],'数据不存在');

        $info = $userInfoBis->getNormalInfoByField('user_id',$id);
        if (empty($info)) return Show::error([],'数据不存在');

        $user['user_info'] = $info;

        return Show::success($user, "OK");
    }
}